/**
 * Created by isoft-work 5 on 12/15/2014.
 */
$(document).ready(function(){
   // render
   loadData();
   // event
   bindEventEdit();
});

function loadData() {
   var _maTV = ISS.SESSION.getSession('session_maTV');

   var user = {};
   user.maTV = _maTV;
   ISS.BACKEND.request({
      url: ISS.SESSION.getSession('contextPath') + '/service/admin/editRegisterMember',
      headers: {
         'Accept': 'application/json',
         'Content-Type': 'application/json'
      },
      type: 'POST',
      dataType: 'json',
      data: JSON.stringify(user),
      success: function (data) {
         var responseContent = data.responseObject;
         loadDataComplete(responseContent);
      }
   });
}

function loadDataComplete(user){
   ISS.DOM.setVal('#RegeditFormMember_code', user.maTV);
   if(user.soDoCoThe) {
      ISS.DOM.setVal('#RegeditFormMember_measurement', user.soDoCoThe.maSDCoThe);
   }
   else {
      ISS.DOM.setVal('#RegeditFormMember_measurement', '');
   }
   ISS.DOM.setVal('#RegeditFormMember_lastname', user.ho);
   ISS.DOM.setVal('#RegeditFormMember_firstname', user.ten);
   ISS.DATE.getFullDate(ISS.DOM.setVal('#RegeditFormMember_birthday', user.ngaySinh));
   ISS.DOM.setVal('#RegeditFormMember_sexual', user.gioiTinh);
   ISS.DOM.setVal('#RegeditFormMember_address', user.diaChi);
   ISS.DOM.setVal('#RegeditFormMember_phonenumber', user.soDT);
}

function bindEventEdit(){
   ISS.EVENT.bindEvent('#RegeditFormMember_edit', 'click', function () {
      sendInfoEdit();
   });
}

function sendInfoEdit() {
   var user = {};
   var measurement = {};
   user.maTV = ISS.DOM.getVal('#RegeditFormMember_code');
   measurement.maSDCoThe = ISS.DOM.getVal('#RegeditFormMember_measurement');
   user.soDoCoThe = measurement;
   user.ten = ISS.DOM.getVal('#RegeditFormMember_firstname');
   user.ho = ISS.DOM.getVal('#RegeditFormMember_lastname');
   user.ngaySinh = ISS.DOM.getVal('#RegeditFormMember_birthday');
   user.gioiTinh = ISS.DOM.getVal('#RegeditFormMember_sexual');
   user.email = ISS.DOM.getVal('#RegeditFormMember_email');
   user.soDT = ISS.DOM.getVal('#RegeditFormMember_phonenumber');
   user.diaChi = ISS.DOM.getVal('#RegeditFormMember_address');

   ISS.BACKEND.request({
      url: ISS.SESSION.getSession('contextPath') + '/service/admin/registerMemberEdit',
      headers: {
         'Accept': 'application/json',
         'Content-Type': 'application/json'
      },
      type: 'POST',
      dataType: 'json',
      data: JSON.stringify(user),
      success: function (data) {
         var responseContent = data.responseObject;
         sendInfoEditComplete(responseContent);
      }
   });
}

function sendInfoEditComplete(user) {
   if(user) {
      ISS.WINDOW.location('/jsp/admin/ad-member-common.jsp');
   }
   else{
      alert('Cập nhật không thành công');
   }
}
